package com.sfy.mallchat.common.common.utils;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.lang.reflect.Method;
import java.util.Optional;

public class SpElUtil {

    private static final ExpressionParser PARSER = new SpelExpressionParser();
    private static final DefaultParameterNameDiscoverer PARAMETER_NAME_DISCOVERER = new DefaultParameterNameDiscoverer();

    public static String getMethodKey(Method method){
        return method.getDeclaringClass()+"#"+method.getName();
    }

    public static String parseSpEl(Method method, Object[] args, String spEL) {
        String[] param = Optional
                .ofNullable(PARAMETER_NAME_DISCOVERER.getParameterNames(method))
                .orElse(new String[]{});
        EvaluationContext context = new StandardEvaluationContext();
        for (int i = 0;i<param.length;i++){
            context.setVariable(param[i],args[i]);
        }
        Expression expression = PARSER.parseExpression(spEL);
        return expression.getValue(context,String.class);

    }
}
